package com.hongyi.controller;

import com.hongyi.common.Result;
import com.hongyi.pojo.Dep;
import com.hongyi.pojo.PageBean;
import com.hongyi.service.DepService;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author hongyi
 * @date 2023/11/22 21:16
 */
@RestController
@RequestMapping("/department")
@MapperScan("com.hongyi.mapper")
public class DepController {
    @Autowired
    private DepService depService;

    @PostMapping("/add")
    public Result add(@RequestBody Dep dep){
        depService.add(dep);
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Dep dep) {
        depService.update(dep);
        return Result.success("修改成功");
    }


    @GetMapping("/find")
    public Result getNameAndAddress(Dep dep) {
        List<Dep> depDto1  = depService.getNameAndAddress(dep);
        return Result.success(depDto1);
    }

    @GetMapping("/fin1")
    public Result getMapId(Long id) {
        Map<String, Object> map= depService.getMapId(id);
        return Result.success(map);
    }

    @GetMapping("/find2")
    public Map<String, Object> getMapId1(Long id) {
        Map<String, Object> map= depService.getMapId(id);
        return map;
    }

    /** find2 得到的结果
     {
     "address": "黄埔",
     "sex": 1,
     "name": "小小",
     "id": 2368,
     "iphone": "17395857854",
     "person_id": 2
     }
     */
     // find1 find2 find3 其实跟根据ID查询实体对象的效果是一样的（一个根据ID进行查询）


    /**
     *
     * @param dep
     * @return
     */
    @GetMapping("/find3")
    public Result getMapId2(Dep dep) {
        Map<String, Object> map = depService.getMapDep(dep);
        return Result.success(map);
    }

  /*  @DeleteMapping("/delete")
    public Result delete(int id) {
        depService.delete(id);
        return Result.success(id);
    }*/
//  @DeleteMapping("/{id}")
//  public Result delete(@PathVariable("id") int id) {
//      depService.delete(id);
//      return Result.success(id);
//  }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id) {
        depService.delete(id);
        return Result.success("删除成功");
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable int id) {
        Dep byId = depService.findById(id);
        return Result.success(byId);
    }
    @GetMapping("/page")
    public PageBean findPage(int currentPage, int PageSize) {
        PageBean<Dep> depPageBean = depService.selectByPage(currentPage, PageSize);
        return depPageBean;
    }
    @GetMapping("/page01")
    public PageBean findPage01(@RequestParam("name") String name,@RequestParam("page") int currentPage, @RequestParam("size") int PageSize) { // 这样其实可以使用参数 @RequestParam
        PageBean<Dep> depPageBean = depService.selectByPage01(name, currentPage, PageSize);
        return depPageBean;
    }
    @GetMapping("/page02")
    public PageBean findPage02( String name, int currentPage, int PageSize) { // 这样其实可以使用参数 @RequestParam
        PageBean<Dep> depPageBean = depService.selectByPage01(name, currentPage, PageSize);
        return depPageBean;
    }

}
